﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Data;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MyCalManager.Data;
using MyCalManager.Helper;
using MyCalManager.Model;
using WPFLocalization;

namespace MyCalManager.ViewModel
{
  
    public class MainContentModel : ViewModelBase
    {

        private CoreData data = CoreData.Instance;
        private bool ValidAddFood = false;
        private bool ValidDelFood = false;
        private bool CanUpdateUserDB = false;
        private bool CanDeleteUserDB = false;

        private WWPointsClass WW = new WWPointsClass();
        private ObservableCollection<Foodstuff> _MyFoods;
        

        /// <summary>
        /// Initializes a new instance of the MainModel class.
        /// </summary>
        public MainContentModel()
        {
            Initialize();
        }

        private void Initialize()
        {
            Messenger.Default.Register<Navigator>(
               this,
               m =>
               {
                   OnNavigator(m);
               });

            Messenger.Default.Register<EditType>(
               this,
               m =>
               {
                   OnEdit(m);
               });

            this._MyFoods = this.data.Foods;
            this.Foods.Source = this._MyFoods;
            this.Categories = this.data.Categories;
            this.ListOfDay = this.data.DayList;
            if (this.ListOfDay != null)
            {
                if (this.ListOfDay.Count > 0)
                    this.selectedDay = this.ListOfDay.Last();
            }
            if (this.data.UserID == -1 || string.IsNullOrEmpty(this.data.GetCurrentUserName()))
            {
                CustomDialogMessage m = new CustomDialogMessage();
                m.isDialog = false;
                m.Content = LocalizationManager.ResourceManager.GetObject("msg1").ToString();
                m.customMessageBoxImage = CustomMessageBoxImage.Information;
                Messenger.Default.Send(m);
            }
            else
            {
                CustomDialogMessage m = new CustomDialogMessage();
                m.isDialog = false;
                m.Content = string.Format(LocalizationManager.ResourceManager.GetObject("msg6").ToString(), this.data.GetCurrentUserName());
                m.customMessageBoxImage = CustomMessageBoxImage.Information;
                Messenger.Default.Send(m);
            }

            // Commands
            this.Add = new RelayCommand(
                () => OnAdd(),
                () => Valid);

            this.AddFood = new RelayCommand(
                () => OnAddFood(),
                () => ValidAddFood);

            this.DelFood = new RelayCommand(
                () => OnDelFood(),
                () => ValidDelFood);

            this.AddCatToggle = new RelayCommand(
                () => OnAddCatToggle());

            this.NewDay = new RelayCommand(
                () => OnNewDay());

            this.UpdateUserDB = new RelayCommand(
                () => OnUpdateUserDB(),
                () => CanUpdateUserDB);

            this.DeleteUserDB = new RelayCommand(
                () => OnDeleteUserDB(),
                () => CanDeleteUserDB);

        }

        

        private void SetValidAddFood()
        {
            bool cat = false;
            if (this.CatComboBox && this.SelectedCategorie != null)
            {
                cat = true;
            }
            else if (this.CatTextBox && this.RubrikValid)
            {
                cat = true;
            }

            this.ValidAddFood = cat && EiweißValid && KhValid && FatValid && KCalValid && ItemValid;
        }

        private void OnNavigator(Navigator n)
        {
            if (n.Result == NavigateResult.UserChanged || n.Result == NavigateResult.DayUpdated)
            {
                this.ListOfDay = this.data.DayList;
                this.Type = FoodType.Breakfast;
            }
            else if (n.Result == NavigateResult.DayAdded)
            {
                this.ListOfDay = this.data.DayList;
            }
        }

        private void OnEdit(EditType m)
        {
            switch (m)
            {
                case EditType.UserDB:
                    this.UserEditVisibility = !this.UserEditVisibility;
                    break;
                case EditType.FoodDB:
                    this.FoodEditVisibility = !this.FoodEditVisibility;
                    break;
                default:
                    break;
            }
        }       

        #region Commands and CommandMethods

        public RelayCommand Add
        {
            get;
            private set;
        }

        private void OnAdd()
        {
            FoodItem newItem = new FoodItem();
            newItem.Eiweiss = Extensions.dreisatz((decimal)this.SelectedFood.Carbs, decimal.Parse(this.Amount));
            newItem.Fett = Extensions.dreisatz((decimal)this.SelectedFood.Fat, decimal.Parse(this.Amount));
            newItem.Kh = Extensions.dreisatz((decimal)this.SelectedFood.Kh, decimal.Parse(this.Amount));
            newItem.Kal = Extensions.dreisatz((decimal)this.SelectedFood.Kcal, decimal.Parse(this.Amount));
            newItem.WWPoints = Extensions.dreisatz((decimal)this.SelectedFood.WWPoints, decimal.Parse(this.Amount));
            newItem.Item = this.SelectedFood.Item;
            newItem.Menge = decimal.Parse(this.Amount);

            if (this.data.AddItem(this.Type, newItem))
            {
                this.data.UpdateDay();
                this.selectedDay.SumWWPoints = this.data.GetSumWWOfDay();
                this.selectedDay.SumKal = this.data.GetSumKalOfDay();
                this.CalOfDay = LocalizationManager.ResourceManager.GetObject("msg4").ToString() + this.selectedDay.SumKal.ToString();
                this.WWOfDay = LocalizationManager.ResourceManager.GetObject("WWLbl").ToString() + " " + this.selectedDay.SumWWPoints.ToString();
                this.UserFoods = this.data.GetFoodCollection(this.Type);

                this.Amount = string.Empty;

                CustomDialogMessage m = new CustomDialogMessage();
                m.isDialog = false;
                m.Content = LocalizationManager.ResourceManager.GetObject("msg5").ToString();
                m.customMessageBoxImage = CustomMessageBoxImage.Information;
                Messenger.Default.Send(m);
            }
            else
            {
                CustomDialogMessage m = new CustomDialogMessage();
                m.isDialog = false;
                m.Content = LocalizationManager.ResourceManager.GetObject("error1").ToString() + this.data.LastError;
                m.customMessageBoxImage = CustomMessageBoxImage.Error;
                Messenger.Default.Send(m);
            }

        }

        public RelayCommand AddFood
        {
            get;
            private set;
        }

        private void OnAddFood()
        {
            string cat;
            if (this.CatComboBox)
            {
                cat = this.SelectedCategorie;
            }
            else
            {
                cat = this.Rubrik;
            }
            CustomDialogMessage m = new CustomDialogMessage();
            Foodstuff f = new Foodstuff();
            f.Item = this.Item;
            f.Kcal = int.Parse(this.KCal);
            f.Fat = float.Parse(this.Fat);
            f.Kh = float.Parse(this.Kh);
            f.Carbs = float.Parse(this.Eiweiß);
            f.Category = cat;

            if (this.data.AddFood(f))
            {
                m.Content = LocalizationManager.ResourceManager.GetObject("msg5").ToString();
                m.customMessageBoxImage = CustomMessageBoxImage.Information;
                this._MyFoods.Add(f);
                this.SelectedFood = f;
            }
            else
            {
                m.Content = LocalizationManager.ResourceManager.GetObject("error1").ToString();
                m.customMessageBoxImage = CustomMessageBoxImage.Error;
            }
            Messenger.Default.Send(m);
        }

        public RelayCommand DelFood
        {
            get;
            private set;
        }

        private void OnDelFood()
        {

            var message = new CustomDialogMessage(LocalizationManager.ResourceManager.GetObject("DelItem").ToString() + this.SelectedFood.Item, CustomMessageBoxImage.Question, CustomMessageBoxButton.YesNo, DialogMessageCallback);
            message.isDialog = true;
            Messenger.Default.Send(message);
        }

        private void DialogMessageCallback(CustomDialogMessageResult result)
        {
            if (result == CustomDialogMessageResult.Yes)
            {
                if (this.data.DeleteFood(this.SelectedFood))
                {
                    this._MyFoods.Remove(this.SelectedFood);
                }
            }

        }

        public RelayCommand AddCatToggle
        {
            get;
            private set;
        }

        private void OnAddCatToggle()
        {
            this.CatTextBox = !this.CatTextBox;
            this.CatComboBox = !this.CatComboBox;
        }

        public RelayCommand NewDay
        {
            get;
            private set;
        }

        private void OnNewDay()
        {
            Navigator n = new Navigator();
            n.To = NavigateTo.NewDay;
            n.Result = NavigateResult.None;
            Messenger.Default.Send(n);
        }

        public RelayCommand UpdateUserDB
        {
            get;
            private set;
        }

        private void OnUpdateUserDB()
        {
            this.SelectedUserFood.Kal = decimal.Parse(this.ChangeKCal);
            this.SelectedUserFood.Fett = decimal.Parse(this.ChangeFat);
            this.SelectedUserFood.Kh = decimal.Parse(this.ChangeKh);
            this.SelectedUserFood.Eiweiss = decimal.Parse(this.ChangeEiweiß);
            this.SelectedUserFood.WWPoints = decimal.Parse(this.ChangeWWPoints);
            this.SelectedUserFood.Menge = decimal.Parse(this.ChangeAmount);

            if (this.data.UpdateItem(this.Type, this.SelectedUserFood))
            {
                this.data.UpdateDay();
                this.selectedDay.SumWWPoints = this.data.GetSumWWOfDay();
                this.selectedDay.SumKal = this.data.GetSumKalOfDay();
                this.CalOfDay = LocalizationManager.ResourceManager.GetObject("msg4").ToString() + this.selectedDay.SumKal.ToString();
                this.WWOfDay = LocalizationManager.ResourceManager.GetObject("WWLbl").ToString() + " " + this.selectedDay.SumWWPoints.ToString();

                this.UserFoods = this.data.GetFoodCollection(this.Type);
                
            }
        }

        public RelayCommand DeleteUserDB
        {
            get;
            private set;
        }

        private void OnDeleteUserDB()
        {
            var message = new CustomDialogMessage(LocalizationManager.ResourceManager.GetObject("DelItem").ToString() + this.SelectedUserFood.Item, CustomMessageBoxImage.Question, CustomMessageBoxButton.YesNo, OnDeleteUserDBCallback);
            message.isDialog = true;
            Messenger.Default.Send(message);
            
        }

        private void OnDeleteUserDBCallback(CustomDialogMessageResult result)
        {
            if (result == CustomDialogMessageResult.Yes)
            {
                if (this.data.DeleteItem(this.Type, this.SelectedUserFood))
                {
                    this.UserFoods = this.data.GetFoodCollection(this.Type);
                    this.WeightOfDay = string.Format("{0} Kg", this.selectedDay.Gewicht);
                    this.CalOfDay = LocalizationManager.ResourceManager.GetObject("msg4").ToString() + this.selectedDay.SumKal.ToString();
                    this.WWOfDay = LocalizationManager.ResourceManager.GetObject("WWLbl").ToString() + " " + this.selectedDay.SumWWPoints.ToString();
                }
            }

        }

        #endregion

        #region Propertys

        public const string FoodsPropertyName = "Foods";
        private CollectionViewSource _Foods = new CollectionViewSource();
        public CollectionViewSource Foods
        {
            get
            {
                return _Foods;
            }

            set
            {
                if (_Foods == value)
                {
                    return;
                }


                _Foods = value;

                
                // Update bindings, no broadcast
                RaisePropertyChanged(FoodsPropertyName);

            }
        }

       
        public const string SearchFoodPropertyName = "SearchFood";
        private string _mySearchFood = string.Empty;
        public string SearchFood
        {
            get
            {
                return _mySearchFood;
            }

            set
            {
                if (_mySearchFood == value)
                {
                    return;
                }
                
                _mySearchFood = value;

                if (string.IsNullOrEmpty(value))
                    this.Foods.View.Filter = null;
                else
                    this.Foods.View.Filter = i => ((Foodstuff)i).Item.ToUpper().Contains(value.ToUpper());
               
                // Update bindings, no broadcast
                RaisePropertyChanged(SearchFoodPropertyName);

               
            }

        }


        public const string SelectedFoodPropertyName = "SelectedFood";
        private Foodstuff _SelectedFood;
        public Foodstuff SelectedFood
        {
            get
            {
                return _SelectedFood;
            }

            set
            {
                if (_SelectedFood == value)
                {
                    return;
                }

                if (value == null)
                    this.ValidDelFood = false;
                else
                    this.ValidDelFood = true;

                _SelectedFood = value;
                
                // Update bindings, no broadcast
                RaisePropertyChanged(SelectedFoodPropertyName);
            }
        }

        public const string FoodEditVisibilityPropertyName = "FoodEditVisibility";
        private bool _myFoodEditVisibility = false;
        public bool FoodEditVisibility
        {
            get
            {
                return _myFoodEditVisibility;
            }

            set
            {
                if (_myFoodEditVisibility == value)
                {
                    return;
                }

                _myFoodEditVisibility = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(FoodEditVisibilityPropertyName);


            }
        }

       
        public const string AmountPropertyName = "Amount";
        private string _myAmount = string.Empty;
        public string Amount
        {
            get
            {
                return _myAmount;
            }

            set
            {
                if (_myAmount == value)
                {
                    return;
                }

                _myAmount = value;
               
                // Update bindings, no broadcast
                RaisePropertyChanged(AmountPropertyName);

               
            }
        }


        public const string ValidPropertyName = "Valid";
        private bool _myValid = false;
        public bool Valid
        {
            get
            {
                return _myValid;
            }

            set
            {
                if (_myValid == value)
                {
                    return;
                }

                
                _myValid = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(ValidPropertyName);
                
            }
        }

        public const string CatComboBoxPropertyName = "CatComboBox";
        private bool _myCatComboBox = true;
        public bool CatComboBox
        {
            get
            {
                return _myCatComboBox;
            }

            set
            {
                if (_myCatComboBox == value)
                {
                    return;
                }


                _myCatComboBox = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(CatComboBoxPropertyName);
                
            }
        }

        public const string CatTextBoxPropertyName = "CatTextBox";
        private bool _myCatTextBox = false;
        public bool CatTextBox
        {
            get
            {
                return _myCatTextBox;
            }

            set
            {
                if (_myCatTextBox == value)
                {
                    return;
                }


                _myCatTextBox = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(CatTextBoxPropertyName);

            }
        }

        

        
        public const string CategoriesPropertyName = "Categories";
        private ObservableCollection<string> _myCategories;
        public ObservableCollection<string> Categories
        {
            get
            {
                return _myCategories;
            }

            set
            {
                if (_myCategories == value)
                {
                    return;
                }

                _myCategories = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(CategoriesPropertyName);

            }
        }

        public const string SelectedCategoriePropertyName = "SelectedCategorie";
        private string _mySelectedCategorie;
        public string SelectedCategorie
        {
            get
            {
                return _mySelectedCategorie;
            }

            set
            {
                if (_mySelectedCategorie == value)
                {
                    return;
                }

                _mySelectedCategorie = value;
                SetValidAddFood();

                // Update bindings, no broadcast
                RaisePropertyChanged(SelectedCategoriePropertyName);

            }
        }

        public const string ItemValidPropertyName = "ItemValid";
        private bool _myItemValid = false;
        public bool ItemValid
        {
            get
            {
                return _myItemValid;
            }

            set
            {
                if (_myItemValid == value)
                {
                    return;
                }


                _myItemValid = value;
                SetValidAddFood();

                // Update bindings, no broadcast
                RaisePropertyChanged(ItemValidPropertyName);

            }
        }

        public const string KCalValidPropertyName = "KCalValid";
        private bool _myKCalValid = false;
        public bool KCalValid
        {
            get
            {
                return _myKCalValid;
            }

            set
            {
                if (_myKCalValid == value)
                {
                    return;
                }
                                
                _myKCalValid = value;
                SetValidAddFood();

                // Update bindings, no broadcast
                RaisePropertyChanged(KCalValidPropertyName);

            }
        }

        public const string FatValidPropertyName = "FatValid";
        private bool _myFatValid = false;
        public bool FatValid
        {
            get
            {
                return _myFatValid;
            }

            set
            {
                if (_myFatValid == value)
                {
                    return;
                }

                
                _myFatValid = value;
                SetValidAddFood();

                // Update bindings, no broadcast
                RaisePropertyChanged(FatValidPropertyName);

            }
        }

        public const string KhValidPropertyName = "KhValid";
        private bool _myKhValid = false;
        public bool KhValid
        {
            get
            {
                return _myKhValid;
            }

            set
            {
                if (_myKhValid == value)
                {
                    return;
                }

                
                _myKhValid = value;
                SetValidAddFood();

                // Update bindings, no broadcast
                RaisePropertyChanged(KhValidPropertyName);

            }
        }

        public const string EiweißValidPropertyName = "EiweißValid";
        private bool _myEiweißValid = false;
        public bool EiweißValid
        {
            get
            {
                return _myEiweißValid;
            }

            set
            {
                if (_myEiweißValid == value)
                {
                    return;
                }

                
                _myEiweißValid = value;
                SetValidAddFood();

                // Update bindings, no broadcast
                RaisePropertyChanged(EiweißValidPropertyName);

            }
        }

        public const string RubrikValidPropertyName = "RubrikValid";
        private bool _myRubrikValid = false;
        public bool RubrikValid
        {
            get
            {
                return _myRubrikValid;
            }

            set
            {
                if (_myRubrikValid == value)
                {
                    return;
                }

                
                _myRubrikValid = value;
                SetValidAddFood();

                // Update bindings, no broadcast
                RaisePropertyChanged(RubrikValidPropertyName);

            }
        }

        public const string ItemdPropertyName = "Item";
        private string _myItem = string.Empty;
        public string Item
        {
            get
            {
                return _myItem;
            }

            set
            {
                if (_myItem == value)
                {
                    return;
                }


                _myItem = value;
                

                // Update bindings, no broadcast
                RaisePropertyChanged(ItemdPropertyName);

            }
        }

        public const string KCalPropertyName = "KCal";
        private string _myKCal = string.Empty;
        public string KCal
        {
            get
            {
                return _myKCal;
            }

            set
            {
                if (_myKCal == value)
                {
                    return;
                }


                _myKCal = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(KCalPropertyName);

            }
        }

        public const string FatPropertyName = "Fat";
        private string _myFat = string.Empty;
        public string Fat
        {
            get
            {
                return _myFat;
            }

            set
            {
                if (_myFat == value)
                {
                    return;
                }


                _myFat = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(FatPropertyName);

            }
        }

        public const string KhPropertyName = "Kh";
        private string _myKh = string.Empty;
        public string Kh
        {
            get
            {
                return _myKh;
            }

            set
            {
                if (_myKh == value)
                {
                    return;
                }


                _myKh = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(KhPropertyName);

            }
        }

        public const string EiweißPropertyName = "Eiweiß";
        private string _myEiweiß = string.Empty;
        public string Eiweiß
        {
            get
            {
                return _myEiweiß;
            }

            set
            {
                if (_myEiweiß == value)
                {
                    return;
                }


                _myEiweiß = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(EiweißPropertyName);

            }
        }

        public const string RubrikPropertyName = "Rubrik";
        private string _myRubrik = string.Empty;
        public string Rubrik
        {
            get
            {
                return _myRubrik;
            }

            set
            {
                if (_myRubrik == value)
                {
                    return;
                }


                _myRubrik = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(RubrikPropertyName);

            }
        }

        public const string ListOfDayPropertyName = "ListOfDay";
        private ObservableCollection<Day> _myListOfDay ;
        public ObservableCollection<Day> ListOfDay
        {
            get
            {
                return _myListOfDay;
            }

            set
            {
                if (_myListOfDay == value)
                {
                    return;
                }

                
                _myListOfDay = value;

                if (value != null && value.Count > 0)
                    this.selectedDay = this.ListOfDay.Last();

                // Update bindings, no broadcast
                RaisePropertyChanged(ListOfDayPropertyName);
               
            }
        }

        public const string selectedDayPropertyName = "selectedDay";
        private Day _myselectedDay;
        public Day selectedDay
        {
            get
            {
                return _myselectedDay;
            }

            set
            {
                if (_myselectedDay == value)
                {
                    return;
                }


                _myselectedDay = value;
                if (this.ListOfDay.Count == 0)
                {
                    this.data.CurrentDay = -1;
                    this.UserFoods = null;
                }
                else
                {
                    this.data.CurrentDay = value.ID;
                    this.UserFoods = this.data.GetFoodCollection(this.Type);
                    this.WeightOfDay = string.Format("{0} Kg", this.selectedDay.Gewicht);
                    this.CalOfDay = LocalizationManager.ResourceManager.GetObject("msg4").ToString() + this.selectedDay.SumKal.ToString();
                    this.WWOfDay = LocalizationManager.ResourceManager.GetObject("WWLbl").ToString() + " " + this.selectedDay.SumWWPoints.ToString();
                }

                // Update bindings, no broadcast
                RaisePropertyChanged(selectedDayPropertyName);
               
            }
        }


        #region UserView

        public const string TypePropertyName = "Type";
        private FoodType _myType = FoodType.Breakfast;
        public FoodType Type
        {
            get
            {
                return _myType;
            }

            set
            {
                if (_myType == value)
                {
                    return;
                }

                _myType = value;
                this.UserFoods = this.data.GetFoodCollection(value);

                // Update bindings, no broadcast
                RaisePropertyChanged(TypePropertyName);

            }
        }


        public const string UserFoodsPropertyName = "UserFoods";
        private ObservableCollection<FoodItem> _myUserFoods;
        public ObservableCollection<FoodItem> UserFoods
        {
            get
            {
                return _myUserFoods;
            }

            set
            {
                if (_myUserFoods == value)
                {
                    return;
                }

                _myUserFoods = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(UserFoodsPropertyName);

            }
        }

        public const string SelectedUserFoodPropertyName = "SelectedUserFood";
        private FoodItem _mySelectedUserFood;
        public FoodItem SelectedUserFood
        {
            get
            {
                return _mySelectedUserFood;
            }

            set
            {
                if (_mySelectedUserFood == value)
                {
                    return;
                }

                _mySelectedUserFood = value;

                if (value != null)
                {
                    this.ChangeItem = value.Item;
                    this.ChangeAmount = value.Menge.ToString();
                    this.ChangeKCal = value.Kal.ToString();
                    this.ChangeFat = value.Fett.ToString();
                    this.ChangeKh = value.Kh.ToString();
                    this.ChangeEiweiß = value.Eiweiss.ToString();
                    this.ChangeWWPoints = value.WWPoints.ToString();
                    CanDeleteUserDB = true;
                }
                else
                {
                    this.ChangeItem = string.Empty;
                    this.ChangeAmount = string.Empty;
                    this.ChangeKCal = string.Empty;
                    this.ChangeFat = string.Empty;
                    this.ChangeKh = string.Empty;
                    this.ChangeEiweiß = string.Empty;
                    this.ChangeWWPoints = string.Empty;
                    CanDeleteUserDB = false;
                }

                // Update bindings, no broadcast
                RaisePropertyChanged(SelectedUserFoodPropertyName);

            }
        }


        public const string WeightOfDayPropertyName = "WeightOfDay";
        private string _myWeightOfDay = "0 Kg";
        public string WeightOfDay
        {
            get
            {
                return _myWeightOfDay;
            }

            set
            {
                if (_myWeightOfDay == value)
                {
                    return;
                }

                _myWeightOfDay = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(WeightOfDayPropertyName);

            }
        }

        public const string CalOfDayPropertyName = "CalOfDay";
        private string _myCalOfDay = "Calorie of the Day : 0";
        public string CalOfDay
        {
            get
            {
                return _myCalOfDay;
            }

            set
            {
                if (_myCalOfDay == value)
                {
                    return;
                }

                _myCalOfDay = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(CalOfDayPropertyName);

            }
        }

        public const string WWOfDayPropertyName = "WWOfDay";
        private string _myWWOfDay = "WWPoints : 0";
        public string WWOfDay
        {
            get
            {
                return _myWWOfDay;
            }

            set
            {
                if (_myWWOfDay == value)
                {
                    return;
                }

                _myWWOfDay = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(WWOfDayPropertyName);

            }
        }

        public const string ChangeItemPropertyName = "ChangeItem";
        private string _myChangeItem = string.Empty;
        public string ChangeItem
        {
            get
            {
                return _myChangeItem;
            }

            set
            {
                if (_myChangeItem == value)
                {
                    return;
                }

                
                _myChangeItem = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(ChangeItemPropertyName);
                
            }
        }

        public const string ChangeAmountPropertyName = "ChangeAmount";
        private string _myChangeAmount = string.Empty;
        public string ChangeAmount
        {
            get
            {
                return _myChangeAmount;
            }

            set
            {
                if (_myChangeAmount == value)
                {
                    return;
                }


                _myChangeAmount = value;

                decimal menge = 0;
                bool valid = decimal.TryParse(value, out menge) && this.SelectedUserFood != null;

                if (valid)
                {
                    if (value != this.SelectedUserFood.Menge.ToString())
                    {
                        decimal eiweiss = Extensions.dreisatz((decimal)this.SelectedUserFood.Eiweiss, menge, (decimal)this.SelectedUserFood.Menge);
                        decimal fett = Extensions.dreisatz((decimal)this.SelectedUserFood.Fett, menge, (decimal)this.SelectedUserFood.Menge);
                        decimal kh = Extensions.dreisatz((decimal)this.SelectedUserFood.Kh, menge, (decimal)this.SelectedUserFood.Menge);
                        decimal kal = Extensions.dreisatz((decimal)this.SelectedUserFood.Kal, menge, (decimal)this.SelectedUserFood.Menge);
                        decimal ww = Extensions.dreisatz((decimal)this.SelectedUserFood.WWPoints, menge, (decimal)this.SelectedUserFood.Menge);

                        this.ChangeKCal = kal.ToString();
                        this.ChangeFat = fett.ToString();
                        this.ChangeKh = kh.ToString();
                        this.ChangeEiweiß = eiweiss.ToString();
                        this.ChangeWWPoints = ww.ToString();
                        CanUpdateUserDB = true;
                    }
                }
                else
                {
                    CanUpdateUserDB = false;
                }

                // Update bindings, no broadcast
                RaisePropertyChanged(ChangeAmountPropertyName);

            }
        }
        
        public const string ChangeAmountValidPropertyName = "ChangeAmountValid";
        private bool _myChangeAmountValid = false;
        public bool ChangeAmountValid
        {
            get
            {
                return _myChangeAmountValid;
            }

            set
            {
                if (_myChangeAmountValid == value)
                {
                    return;
                }
                
                _myChangeAmountValid = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(ChangeAmountValidPropertyName);
            }
        }

        public const string ChangeKCalPropertyName = "ChangeKCal";
        private string _myChangeKCal = string.Empty;
        public string ChangeKCal
        {
            get
            {
                return _myChangeKCal;
            }

            set
            {
                if (_myChangeKCal == value)
                {
                    return;
                }


                _myChangeKCal = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(ChangeKCalPropertyName);

            }
        }

        public const string ChangeFatPropertyName = "ChangeFat";
        private string _myChangeFat = string.Empty;
        public string ChangeFat
        {
            get
            {
                return _myChangeFat;
            }

            set
            {
                if (_myChangeFat == value)
                {
                    return;
                }


                _myChangeFat = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(ChangeFatPropertyName);

            }
        }

        public const string ChangeKhPropertyName = "ChangeKh";
        private string _myChangeKh = string.Empty;
        public string ChangeKh
        {
            get
            {
                return _myChangeKh;
            }

            set
            {
                if (_myChangeKh == value)
                {
                    return;
                }


                _myChangeKh = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(ChangeKhPropertyName);

            }
        }

        public const string ChangeEiweißPropertyName = "ChangeEiweiß";
        private string _myChangeEiweiß = string.Empty;
        public string ChangeEiweiß
        {
            get
            {
                return _myChangeEiweiß;
            }

            set
            {
                if (_myChangeEiweiß == value)
                {
                    return;
                }


                _myChangeEiweiß = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(ChangeEiweißPropertyName);

            }
        }

        public const string ChangeWWPointsPropertyName = "ChangeWWPoints";
        private string _myChangeWWPoints = string.Empty;
        public string ChangeWWPoints
        {
            get
            {
                return _myChangeWWPoints;
            }

            set
            {
                if (_myChangeWWPoints == value)
                {
                    return;
                }


                _myChangeWWPoints = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(ChangeWWPointsPropertyName);

            }
        }

       
        public const string UserEditVisibilityPropertyName = "UserEditVisibility";
        private bool _myUserEditVisibility = false;
        public bool UserEditVisibility
        {
            get
            {
                return _myUserEditVisibility;
            }

            set
            {
                if (_myUserEditVisibility == value)
                {
                    return;
                }
                
                _myUserEditVisibility = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(UserEditVisibilityPropertyName);

                
            }
        }

        #endregion

        #endregion

    }
}